//
//  WBWelComeViewControlViewController.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/7.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class WBWelComeViewControlViewController: UIViewController {
    // MARK: --懒加载控件
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    fileprivate lazy var userImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        if let imageURL = WBUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            imageView.sd_setImage(with: URL(string:imageURL), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        return imageView
    }()
    
    fileprivate lazy var messageLabel:UILabel = {
       
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        
        if let name = WBUserAccountViewModel.sharedViewModel.userAccount?.name {
            
            label.text = "欢迎回家,\(name)"
            
        }else {
            label.text = "欢迎回家"
        }
        return label
    }()
    
    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startSpringAnimation()
    }
    
    private func setupUI() {
        
        view.addSubview(userImageView)
        
        userImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(self.view).offset(200)
            make.centerX.equalTo(self.view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        view.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(userImageView)
            
        }
    }
    
    private func startSpringAnimation() {
        
        userImageView.snp.updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
        }
        //  开启弹簧动画
        /*
         1.Duration: 动画时长
         2.Damping : 阻尼系数 0-1
         3.Velocity: 初始速度
         4.options : 选项
         5.动画执行的代码块
         6.动画完成执行的代码块
         */
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                
                NotificationCenter.default.post(name: NSNotification.Name( SwitchRootVCNotificationName), object: nil)
            })
        }
        
    }
}
